package com.luojing.springcloud.config.orderdelay;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/** 订单超时执行消息体
 * @author luojing
 * @create 2018/10/29 9:03 AM
 **/
public class OrderDelayMsg implements Delayed {

  private long orderId;

  private String orderStatus;

  private String targetOrderStatus;

  private long executeTime;

  @Override
  public long getDelay(TimeUnit unit) {
    return unit.convert(this.executeTime - System.nanoTime(), TimeUnit.NANOSECONDS);
  }

  @Override
  public int compareTo(Delayed o) {
    OrderDelayMsg orderDelayMsg = (OrderDelayMsg) o;
    return this.orderId > orderDelayMsg.orderId ? 1 : (this.orderId < orderDelayMsg.orderId ? -1 : 0);
  }

  public long getOrderId() {
    return orderId;
  }

  public String getOrderStatus() {
    return orderStatus;
  }

  public String getTargetOrderStatus() {
    return targetOrderStatus;
  }

  public long getExecuteTime() {
    return executeTime;
  }

  public OrderDelayMsg(long orderId, String orderStatus, String targetOrderStatus, long executeTime) {
    this.orderId = orderId;
    this.orderStatus = orderStatus;
    this.targetOrderStatus = targetOrderStatus;
    this.executeTime = executeTime + System.nanoTime();
  }
}
